package org.thesis.secutity;

import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import android.util.Base64;
import android.util.Log;

public class sendingSecurity {
	
	final static String str_key = "MIGfMA0GCSqG()*?.,';`22SIb3DQEB[A4GNAD%&#@CBiQKBgQCXwL7O2-+PpuxMuvBdC^p4t8zQ%9g#oDKEzQ5iFudKwkmeZ1Nkv2Qsb8|ID8wrtBeT_+.hGA35zpse@#eN.1glfhTjHbMI9;BFc+Nw`cfHrKnhJ?ryiLQLAxq6SMmVcZcFRiD4NxPhkZ8Pf/BQSUVIE2j//nmf0H6QfYTUhR1vh4xK3s+CQIDAQAB";
	final static String UTF8 = "UTF-8";
	final static String MD5 = "MD5";
	final static String AES = "AES";
	byte[] key;
    MessageDigest sha;
    SecretKeySpec secretKeySpec;
    Cipher cipher;
    
	public sendingSecurity(){
		try {
			key = str_key.getBytes(UTF8);
			sha = MessageDigest.getInstance(MD5);
			key = sha.digest(key);
			// Generate the secret key specs.
			secretKeySpec = new SecretKeySpec(key, AES);
			// Instantiate the cipher
			cipher = Cipher.getInstance(AES);
		} catch (Exception e) {
			Log.v("sendingSecurity/contructor", "ERROR:" + e);
		}
	}
	
	public String Encrypt(String str_plaintext){
		String str_encrypted = "";
		try{
		cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        byte[] encrypted = cipher.doFinal((str_plaintext).getBytes());               
        str_encrypted = Base64.encodeToString(encrypted, 0);
        Log.v("mahoa=",str_encrypted);
		}catch(Exception e){
			Log.v("sendingSecurity/Encrypt", "ERROR:" + e);
		}	
		return str_encrypted;
	}
	
	public String Decrypt(String str_encrypted){
		String str_decrypted = "";
		try{
			byte[] bytemahoa = Base64.decode(str_encrypted, 0);
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            byte[] decrypted = cipher.doFinal(bytemahoa);           
            str_decrypted = new String(decrypted);
        Log.v("giaima=",str_decrypted);
		}catch(Exception e){
			Log.v("sendingSecurity/Decrypt", "ERROR:" + e);
		}	
		return str_decrypted;
	}
}
